package cn.hdustea.shopNavigation.ui.view.bottom

import android.Manifest
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.fragment.app.Fragment
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.adapter.BeautyItemAdapter
import cn.hdustea.shopNavigation.callback.CallBackManager
import cn.hdustea.shopNavigation.callback.CallBackType
import cn.hdustea.shopNavigation.callback.IGlobalCallback
import cn.hdustea.shopNavigation.config.ConfigKey
import cn.hdustea.shopNavigation.config.ConstantUrl
import cn.hdustea.shopNavigation.entity.BannerEntity
import cn.hdustea.shopNavigation.entity.Beauty
import cn.hdustea.shopNavigation.ui.contract.BeautyContract
import cn.hdustea.shopNavigation.ui.presenter.BeautyPresenter
import cn.hdustea.shopNavigation.ui.view.MarketSelectActivity
import cn.hdustea.shopNavigation.ui.view.SearchActivity
import cn.hdustea.shopNavigation.ui.view.main.*
import cn.hdustea.shopNavigation.ui.view.main.amusement.AmusementActivity
import cn.hdustea.shopNavigation.ui.view.main.beauty.BeautyActivity
import cn.hdustea.shopNavigation.widget.scanner.ScanActivity
import com.bumptech.glide.Glide
import com.hdustea.base.base.BaseFragment
import com.hdustea.base.util.PermissionUtil
import com.hdustea.base.util.ToastUtil
import com.hdustea.base.widget.CustomLoadMoreView
import kotlinx.android.synthetic.main.fragment_beauty.*
import kotlinx.android.synthetic.main.fragment_main.*

/**
 * <pre>
 *     author : HuQiang
 *     time   : 2020/09/18
 *     desc   :
 *     version: 1.0
 * </pre>
 */
class HomeFragment : BaseFragment<BeautyContract.Presenter>(), BeautyContract.View, View.OnFocusChangeListener {

    private var beautyAdapter: BeautyItemAdapter? = null
    private var mCurrentPage: Int = 0


    companion object {
        fun getInstance(): HomeFragment = HomeFragment()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_main, container, false)
    }





    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mPresenter?.getBeautyList(0, false)
        initView()
        initBanner()
    }

    override fun initView() {
        icon_top_scan.setOnClickListener {
            CallBackManager.getInstance()
                    .addCallback(CallBackType.ON_SCAN, object : IGlobalCallback<String> {
                        override fun executeCallBack(args: String?) {
                            ToastUtil.showShort(args)
                        }
                    })
            PermissionUtil.permissionRequest(context, {
                ScanActivity.startActivity(context!!)
            }, arrayOf(Manifest.permission.CAMERA))
        }

        home_search_view.onFocusChangeListener = this

        im_food.setOnClickListener {
            FoodActivity.startActivity(context!!)
        }

        im_beauty.setOnClickListener {
            BeautyActivity.startActivity(context!!)
        }

        im_special.setOnClickListener {
            StoreActivity.startActivity(context!!)
        }

        im_live.setOnClickListener {
            LifeActivity.startActivity(context!!)
        }

        im_play.setOnClickListener {
            AmusementActivity.startActivity(context!!)
        }

        im_mother.setOnClickListener {
            ChildActivity.startActivity(context!!)
        }

        im_education.setOnClickListener {
            EducationActivity.startActivity(context!!)
        }

        tianjie_text.setOnClickListener {
            MarketSelectActivity.startActivity(context!!)
        }

    }

    private fun initBanner() {
        val banners = mutableListOf<BannerEntity>()
        for (i in 1..3) {
            val banner = BannerEntity(id = i.toString(),
                    bannerPath = "${ConstantUrl.BASE_URL}/image/banner/main$i.jpg",
                    url = null)
            banners.add(banner)
        }
        im_banner.setBannerData(banners)
        im_banner.loadImage { _, model, view, _ ->
            Glide.with(this)
                    .load((model as BannerEntity).bannerPath)
                    .into(view as AppCompatImageView)
        }
    }

    override fun onResume() {
        super.onResume()
        if (ConfigKey.getGlobalLocation() == "") {
            tianjie_text.text = "龙湖天街"
        } else {
            tianjie_text.text = ConfigKey.getGlobalLocation()
        }
    }

     override fun onFocusChange(view: View?, hasFocus: Boolean) {
        if (hasFocus) {
            home_search_view.clearFocus()
            SearchActivity.startActivity(context!!)
        }
    }

    override fun getLayoutRes():Int = R.layout.fragment_main

    override fun getPresenter():BeautyContract.Presenter = BeautyPresenter(this)

    override fun setBeautyList(beautyList: MutableList<Beauty>, isRefresh: Boolean) {
        if (!isRefresh) {
            beautyAdapter = BeautyItemAdapter(R.layout.item_list_beauty, this.context!!)
            initRecyclerView()
            recycler_view_home.adapter = beautyAdapter
            beautyAdapter!!.setList(beautyList)
        } else {
            if (beautyList.isNotEmpty()) {
                mCurrentPage++
                beautyAdapter!!.addData(beautyList)
                beautyAdapter!!.loadMoreModule.loadMoreComplete()
            } else {
                beautyAdapter!!.loadMoreModule.loadMoreEnd()
            }
        }
    }

    private fun initRecyclerView() {
        beautyAdapter!!.animationEnable = true
        beautyAdapter!!.loadMoreModule.loadMoreView = CustomLoadMoreView()
        beautyAdapter!!.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.getBeautyList(mCurrentPage + 1, true)
        }
        beautyAdapter!!.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        beautyAdapter!!.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }


    override fun onError() {
        beautyAdapter!!.loadMoreModule.isEnableLoadMore = true
        beautyAdapter!!.loadMoreModule.loadMoreFail()
    }
}